/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.service.product.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.cereshop.app.dao.product.CereProductClassifyDAO;
import com.shop.cereshop.app.dao.product.CereProductSkuDAO;
import com.shop.cereshop.app.dao.product.CereShopProductDAO;
import com.shop.cereshop.app.dao.product.ProductAveragePriceDAO;
import com.shop.cereshop.app.page.index.ProductClassify;
import com.shop.cereshop.app.service.product.ProductAveragePriceService;
import com.shop.cereshop.commons.domain.product.CereProductClassify;
import com.shop.cereshop.commons.domain.product.ProductAveragePrice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 商品均价统计表
 * </p>
 *
 * @author
 * @date 2025-07-29
 */
@Slf4j
@Service
public class ProductAveragePriceServiceImpl implements ProductAveragePriceService {

    @Resource
    private ProductAveragePriceDAO averagePriceDAO;


    //商品类别
    @Resource
    CereProductClassifyDAO cereProductClassifyDAO;

    //商品信息
    @Resource
    CereProductSkuDAO cereProductSkuDAO;


    @Override
    public void calculateProductAveragePrice() {
        QueryWrapper<CereProductClassify> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classify_level", 3);
        List<CereProductClassify> productClassifyList = cereProductClassifyDAO.selectList(queryWrapper);
        List<ProductAveragePrice> productAveragePriceList = new ArrayList<>();
        productClassifyList.forEach(productClassify -> {
            ProductAveragePrice productAveragePrice = cereProductSkuDAO.selectAvgPriceByClassifyId(productClassify.getClassifyId());
            if (BeanUtil.isNotEmpty(productAveragePrice) && productAveragePrice.getProductCount() > 0) {
                // 处理空值情况，确保所有字段都有有效值
                if (productAveragePrice.getProductCount() == null) {
                    productAveragePrice.setProductCount(0);
                }
                if (productAveragePrice.getTotalPrice() == null) {
                    productAveragePrice.setTotalPrice(BigDecimal.ZERO);
                }
                if (productAveragePrice.getAveragePrice() == null) {
                    productAveragePrice.setAveragePrice(BigDecimal.ZERO);
                }
                if (productAveragePrice.getMinPrice() == null) {
                    productAveragePrice.setMinPrice(BigDecimal.ZERO);
                }
                if (productAveragePrice.getMaxPrice() == null) {
                    productAveragePrice.setMaxPrice(BigDecimal.ZERO);
                }

                productAveragePrice.setClassifyId(productClassify.getClassifyId());
                productAveragePrice.setClassifyName(productClassify.getClassifyName());
                productAveragePrice.setClassifyLevel(productClassify.getClassifyLevel());
                productAveragePrice.setStatisticsDate(LocalDate.now());
                productAveragePrice.setCreateTime(LocalDateTime.now());
                productAveragePrice.setUpdateTime(LocalDateTime.now());
                productAveragePrice.setPriceRange(productAveragePrice.getMinPrice().toString() + "~" + productAveragePrice.getMaxPrice().toString());
                productAveragePriceList.add(productAveragePrice);
            }
        });

        if (!productAveragePriceList.isEmpty()) {
            int delete = averagePriceDAO.delete(null);
            log.info("商品均价统计表删除{}条", delete);
            int i = averagePriceDAO.insertBatch(productAveragePriceList);
            log.info("商品均价统计表插入数据{}条", i);
        }
    }
}
